package net.ncdesign.components.nodeWorkspace.core.base
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	import net.ncdesign.components.nodeWorkspace.core.interfaces.INodeElement;

	[Event(name="linkInPortSelected", type="flash.events.Event")]
	[Event(name="linkOutPortSelected", type="flash.events.Event")]
	[Event(name="nodeStartDrag", type="flash.events.Event")]
	[Event(name="nodeStopDrag", type="flash.events.Event")]
	[Event(name="nodeSelected", type="flash.events.Event")]
	public class BaseNode extends Canvas implements INodeElement
	{
		public static const LINK_IN_PORT_SELECTED:String = 'linkInPortSelected';
		public static const LINK_OUT_PORT_SELECTED:String = 'linkOutPortSelected';
		public static const NODE_DRAG_START:String = 'nodeStartDrag';
		public static const NODE_DRAG_STOP:String = 'nodeStopDrag';
		public static const NODE_SELECTED:String = 'nodeSelected';
		
		private var _nodePosition:NodePosition = new NodePosition(0, 0);
		private var _nodeID:String = '';
		
		[Bindable]
		private var _selected:Boolean = false;
		
		[Bindable]
		public function get selected():Boolean
		{
			return _selected
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			selectedState();
		}
		
		public function BaseNode()
		{
			super();
			this.doubleClickEnabled = true;
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
		}
		
		public function get nodeID():String
		{
			return uid;
		}
		
		public function set nodeID(value:String):void
		{
			_nodeID = value;
		}
		
		[Bindable] private var _nodeIndex:int = 0;
		
		public function get nodeIndex():int
		{
			return _nodeIndex;
		}
		
		public function set nodeIndex(value:int):void
		{
			_nodeIndex = value;;
		}
		
		public function get nodeType():String
		{
			return null;
		}
		
		public function get componentType():String
		{
			return null;
		}
		
		[Bindable]
		public function get nodePosition():NodePosition
		{
			return _nodePosition;
		}
		
		public function set nodePosition(value:NodePosition):void
		{
			_nodePosition = value;
		}
		
		public function get linkInPort():UIComponent
		{
			return null;
		}
		
		public function set linkInPort(value:UIComponent):void
		{
		}
		
		public function get linkOutPort():UIComponent
		{
			return null;
		}
		
		public function set linkOutPort(value:UIComponent):void
		{
		}
		
		public function selectedState():void
		{
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			this.startDrag();
			var eventObj:Event = new Event(NODE_DRAG_START);
			dispatchEvent(eventObj);
			
			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			this.stopDrag();
			nodePosition = new NodePosition(event.currentTarget.x, event.currentTarget.y);
			var eventObj:Event = new Event(NODE_DRAG_STOP);
			dispatchEvent(eventObj);
			removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}		
		
		private function onDoubleClick(event:MouseEvent):void
		{
			var eventObj:Event = new Event(NODE_SELECTED);
			dispatchEvent(eventObj);
		}
		
		private function onMouseMove(event:MouseEvent):void
		{
			nodePosition = new NodePosition(event.currentTarget.x, event.currentTarget.y);
		}
	}
}